import { legacy_createStore as createStore, combineReducers, applyMiddleware, Action } from 'redux'
import { ThunkAction } from 'redux-thunk'
// 引入模块化购物车reducer
import carReducer from './car/carReducer'
// 引入模块化用户reducer
import userReducer from './user/userReducer'
// 引入插件
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web

// 配置
const persistConfig = {
	key: 'root',
	storage,
}

// 合并reducer
const reducer = combineReducers({
	car: carReducer,
	user: userReducer
})

const persistedReducer = persistReducer(persistConfig, reducer)

// 创建仓库
const store = createStore(persistedReducer, applyMiddleware(logger, thunk))
export const persistor = persistStore(store)
// 仓库类型
export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch;

export type AppThunk<ReturnType = void> = ThunkAction<
	ReturnType,
	RootState,
	unknown,
	Action<string>
>;

export default store